using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using GonzalesCore.CoreElements;
using GonzalesCore.CoreManagers;
using Kypris2.CoreElements;
using Kypris2.Utils;
using Jamila2.Tools;

public partial class GonzalesAdminPanelAdminDetail : System.Web.UI.Page
{
    protected PrivateLoginSession CurrentPrivateSession = null;
    protected LogManager CurLogManager = null;

    #region AdminID
    protected string AdminID
    {
        get
        {
            return ViewState["AdminID"].ToString();
        }

        set
        {
            ViewState["AdminID"] = value;
        }
    }
    #endregion

    #region ErrorMessage
    protected string ErrorMessage
    {
        get
        {
            return ViewState["ErrorMessage"].ToString();
        }

        set
        {
            ViewState["ErrorMessage"] = value;
        }
    }
    #endregion

    #region Events
    protected void Page_Load(object sender, EventArgs e)
    {
        this.SessionCheck();

        if (!IsPostBack)
        {
            this.PageComponentSetup();
            this.SwitchLanguage();

            if (Request.QueryString["id"] != null && Request.QueryString["id"].Length == 36)
            {
                this.AdminID = Request.QueryString["id"];
            }

            this.LoadData();

            // log down the history
            this.CurLogManager.LogMessage("[GonzalesAdminPanelAdminDetail] : Admin (" + this.AdminID + ") viewed ", this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.User);

        }
    }
    protected void Button_Submit_Click(object sender, EventArgs e)
    {
        bool smooth = true;

        using (UserManager CurUserManager = new UserManager(this.CurrentPrivateSession))
        {
            if (!(smooth = CurUserManager.UpdateAdmin(
                this.AdminID,
                this.CheckBox_ITAdmin.Checked,
                this.CheckBox_PowerUser.Checked)))
            {
                string LogID = SwissArmy.UniqueID();
                this.CurLogManager.LogError(LogID, "[GonzalesAdminPanelAdminDetail.Button_Submit_Click] : Failed at CurUserManager.GetUserByInternalID('" + this.AdminID + "') : " + CurUserManager.ErrorMessage, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);
                this.ShowErrorMessage(this.ErrorMessage + LogID);
            }
            else
            {
                // log down the history
                this.CurLogManager.LogMessage("[GonzalesAdminPanelAdminDetail] : Admin (" + this.AdminID + ", IT Admin: " + this.CheckBox_ITAdmin.Checked + ", Super Admin: " + this.CheckBox_PowerUser.Checked + ") updated ", this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.User);
                // Added by thangnv on 20130703 -- Log Change
                SystemUser CurSysAddmin = CurUserManager.GetAdminPanelByID(this.AdminID);
                if (CurSysAddmin != null)
                {
                    #region Logged By Role
                    string LoggedByRole = string.Empty;
                    if (this.CurrentPrivateSession.CurrentLoggedOnUser.IsFinanceAdmin)
                    {
                        if (this.CurrentPrivateSession.CurrentLoggedOnUser.IsFinancialManager)
                        {
                            LoggedByRole = GonzalesUserRole.FinanceManager;
                        }
                        else
                        {
                            LoggedByRole = GonzalesUserRole.FinanceAdmin;
                        }
                    }
                    else if (this.CurrentPrivateSession.CurrentLoggedOnUser.IsPowerAdmin)
                    {
                        LoggedByRole = GonzalesUserRole.PowerAdmin;
                    }
                    else if (this.CurrentPrivateSession.CurrentLoggedOnUser.IsITAdmin)
                    {
                        LoggedByRole = GonzalesUserRole.ITAdmin;
                    }
                    #endregion

                    this.CurLogManager.ChangeLogSystemAdmin(this.CurrentPrivateSession.CurrentLoggedOnUser.InternalID, this.CurrentPrivateSession.CurrentLoggedOnUser.DisplayName, CurSysAddmin.EmployeeID, CurSysAddmin.DisplayName,
                        CurSysAddmin.FromCompany.InternalID, CurSysAddmin.FromCompany.CompanyCode, CurSysAddmin.FromCompany.CompanyName, GonzalesUserAction.Modified, CurSysAddmin.FromCompany.Entity.InternalID, CurSysAddmin.InternalID,
                        (CurSysAddmin.IsITAdmin ? 1 : 0), (CurSysAddmin.IsPowerAdmin ? 1 : 0), LoggedByRole, CurSysAddmin.EmailID);
                }
            }
        }

        if (smooth)
        {
            Response.Redirect("GonzalesAdminPanelAdmin.aspx");
        }
    }

    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("GonzalesAdminPanelAdmin.aspx");
    }

    #endregion

    #region Customized Methods

    #region SessionCheck
    public void SessionCheck()
    {

        if (Session[GonzalesCommonNamingConvention.CurrentPrivateSession] != null)
        {
            this.CurrentPrivateSession = (PrivateLoginSession)Session[GonzalesCommonNamingConvention.CurrentPrivateSession];
            this.CurLogManager = new LogManager(this.CurrentPrivateSession);    // activate Log Manager
        }
        else
        {
            Response.Redirect(GonzalesErrorPageRedirect.SessionError);
        }

    }
    #endregion

    #region PageComponentSetup
    protected void PageComponentSetup()
    {

    }
    #endregion

    #region SwitchLanguage
    protected void SwitchLanguage()
    {
        try
        {
            using (MultiLingualFactory CurLanguageFactory = MultiLingualEngine.GetMultiLingualFactory(this, this.CurrentPrivateSession.CurrentLoggedOnUser.PreferedLanguage.InternalID))
            {
                this.Label_Admin.Text = CurLanguageFactory.DisplayValue("Admin");
                this.Label_EditAdmin.Text = CurLanguageFactory.DisplayValue("EditAdmin");
                this.Label_Information.Text = CurLanguageFactory.DisplayValue("Information");
                this.Label_ITAdmin.Text = CurLanguageFactory.DisplayValue("ITAdmin");
                this.Label_PageTitle.Text = CurLanguageFactory.DisplayValue("PageTitle");
                this.Label_PowerUser.Text = CurLanguageFactory.DisplayValue("PowerUser");
                this.Button_Submit.Text = CurLanguageFactory.DisplayValue("Submit");
                this.ErrorMessage = CurLanguageFactory.DisplayValue("ErrorMessage");
            }
        }
        catch (Exception ex)
        {
            this.CurLogManager.LogError("[GonzalesAdminClaimantsDetail.SwitchLanguage.EX] : " + ex.Message, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.User);
        }

    }
    #endregion

    #region ShowErrorMessage
    protected void ShowErrorMessage(string ErrorMessage)
    {
        this.Table_MessageBox.Visible = true;
        this.Table_MessageBox.Rows[0].Cells[0].Text = ErrorMessage;
        this.Table_MessageBox.Rows[0].Cells[0].CssClass = "ErrorBox";
    }
    #endregion

    #region ShowMessage
    protected void ShowMessage(string Message)
    {
        this.Table_MessageBox.Visible = true;
        this.Table_MessageBox.Rows[0].Cells[0].Text = Message;
        this.Table_MessageBox.Rows[0].Cells[0].CssClass = "MessageBox";
    }
    #endregion

    #endregion

    #region Data Operation

    #region LoadData
    protected void LoadData()
    {
        using (UserManager CurUserManager = new UserManager(this.CurrentPrivateSession))
        {
            SystemUser CurrentAdmin = CurUserManager.GetUserByInternalID(this.AdminID);

            if (CurrentAdmin != null)
            {
                this.LabelValue_Approver.Text = CurrentAdmin.DisplayName + " (" + CurrentAdmin.EmailID + ")";
                this.CheckBox_ITAdmin.Checked = CurrentAdmin.IsITAdmin;
                this.CheckBox_PowerUser.Checked = CurrentAdmin.IsPowerAdmin;
            }
            else
            {
                string LogID = SwissArmy.UniqueID();
                this.CurLogManager.LogError(LogID, "[GonzalesAdminPanelAdminDetail.LoadData] : Failed at CurUserManager.GetUserByInternalID('" + this.AdminID + "') : " + CurUserManager.ErrorMessage, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);
                this.ShowErrorMessage(this.ErrorMessage + LogID);
            }
        }

    }
    #endregion


    #endregion

}
